<?php
/**
 * Set acl resources and what is allow
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       App_Model_Module_Interface
 * @category   Library
 * @package    App
 * @subpackage Model
 * @todo
 */
class App_Security_Acl
{
    /**
     * Zend acl object
     * 
     * @var Zend_Acl 
     */
    protected $_acl;

    /**
     * Set Zend acl object
     * 
     * @return void 
     */
    public function __construct() {
        $this->_acl = new Zend_Acl();
    }

    /**
     * Set resource for acl object
     *
     * @param App_Model_Module_Interface $moduleObj
     * 
     * @return void
     */
    public function setResource(App_Model_Module_Interface $moduleObj) {
        
        foreach ($moduleObj->getModulesAndControllers() as $module => $controllers) {
            foreach ($controllers as $controller) {
                $resource = $this->_getResourceName($module, $controller);
                $this->_acl->addResource(new Zend_Acl_Resource($resource));
            }
        }
    }

    /**
     * Set access for the user
     * 
     * @param App_Model_Database_Rules_Interface $rules
     * 
     * @return Zend_Acl 
     */
    public function setAllow(App_Model_Database_Rules_Interface $rules)
    {
        foreach ($rules->getAllow() as $resource => $actions) {
            /** Check if module has app.ini for resources data */
            if ($this->_acl->has($resource)) {
                $this->_acl->allow(null, $resource, $actions);
            }
        }
        return $this->_acl;
        
    }

        /**
     * Set admin access
     * 
     * @return Zend_Acl 
     */
    public function setAdmin()
    {
        $this->_acl->allow();
        return $this->_acl;
    }
    
    /**
     * Get resource name of module and controller name
     * 
     * @param string $module
     * @param string $controller
     * 
     * @return string 
     */
    protected function _getResourceName($module, $controller)
    {
        return $module . "_" . $controller;
    }
}